<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Api\V2\DeliveryBoyController;
use App\Models\OrderJob;
use App\Models\SmsTemplate;
use App\Utility\NotificationUtility;
use App\Utility\SmsUtility;
use Illuminate\Http\Request;
use App\Models\OrderDetail;
use App\Models\ProductStock;
use App\Models\Product;
use App\Models\Order;
use App\Models\City;
use App\Models\User;
use App\Models\Address;
use App\Models\Addon;
use Illuminate\Support\Carbon;
use Matrix\Exception;
use Session;
use Auth;
use Mail;
use App\Mail\InvoiceEmailManager;
use App\Http\Resources\PosProductCollection;
use App\Models\Country;
use App\Models\State;
use App\Utility\CategoryUtility;
use function get_setting;

class PosController extends Controller
{
    public function index()
    {
        $customers = User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get();
        if (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
            return view('pos.index', compact('customers'));
        }elseif (Auth::user()->user_type == 'salesman'){
            $customers = User::where('user_type', 'customer')->where('referred_by', '=', Auth::user()->id )->orderBy('created_at', 'desc')->get();
            return view('pos.salesman.index', compact('customers'));
        }
        else {
            if (get_setting('pos_activation_for_seller') == 1) {
                flash(translate('POS is disable for Sellers!!!'))->error();
                return back();
                return view('pos.frontend.seller.pos.index', compact('customers'));
            }
            else {
                flash(translate('POS is disable for Sellers!!!'))->error();
                return back();
            }
        }
    }
    public function cronIndex()
    {
//        $customers = User::where('user_type', 'customer')->where('referred_by', '=', Auth::user()->id )->orderBy('created_at', 'desc')->get();
        $customers = User::where('user_type', 'customer')->orderBy('created_at', 'desc')->get();
        return view('pos.salesman.cronIndex', compact('customers'));
    }

    public function search(Request $request)
    {
        if(Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff'){
//            $products = ProductStock::join('products','product_stocks.product_id', '=', 'products.id')->where('products.added_by', 'admin')->select('products.*','product_stocks.id as stock_id','product_stocks.variant','product_stocks.price as stock_price', 'product_stocks.qty as stock_qty', 'product_stocks.image as stock_image')->orderBy('products.created_at', 'desc');
            $products = ProductStock::join('products','product_stocks.product_id', '=', 'products.id')->select('products.*','product_stocks.id as stock_id','product_stocks.variant','product_stocks.price as stock_price', 'product_stocks.qty as stock_qty', 'product_stocks.image as stock_image')->orderBy('products.created_at', 'desc');
            // $products = Product::where('added_by', 'admin')->where('published', '1');
        }elseif (Auth::user()->user_type == 'salesman'){
            $products = ProductStock::join('products','product_stocks.product_id', '=', 'products.id')->select('products.*','product_stocks.id as stock_id','product_stocks.variant','product_stocks.price as stock_price', 'product_stocks.qty as stock_qty', 'product_stocks.image as stock_image')->orderBy('products.created_at', 'desc');
            $users = User::where('pid', Auth::user()->id)->get()->toArray();
            $products = $products->whereIn('products.user_id', array_column($users, 'id'));
        }
        else {
            $products = ProductStock::join('products','product_stocks.product_id', '=', 'products.id')->where('user_id', Auth::user()->id)->where('published', '1')->select('products.*','product_stocks.id as stock_id','product_stocks.variant','product_stocks.price as stock_price', 'product_stocks.qty as stock_qty', 'product_stocks.image as stock_image')->orderBy('products.created_at', 'desc');
            // $products = Product::where('user_id', Auth::user()->id)->where('published', '1');
        }

        if($request->category != null){
            $arr = explode('-', $request->category);
            if($arr[0] == 'category'){
                $category_ids = CategoryUtility::children_ids($arr[1]);
                $category_ids[] = $arr[1];
                $products = $products->whereIn('products.category_id', $category_ids);
            }
        }
        if($request->user_id != null){
            $products = $products->where('products.user_id', $request->user_id);
        }

        if($request->brand != null){
            $products = $products->where('products.brand_id', $request->brand);
        }

        if ($request->keyword != null) {
            $products = $products->where('products.name', 'like', '%'.$request->keyword.'%')->orWhere('products.barcode', $request->keyword);
        }
        $new_array = $products->paginate(16);
        foreach($new_array as $key => $item){
            if($request->user_id != null){
                $products_original = Product::where('id', $item->original_id)->first();
               $new_array[$key]['products_original_price'] = $products_original->unit_price;

            }
        };
        /*
        $p = $products->get();
        dd($p);
        */
        // dd($new_array);
        $stocks = new PosProductCollection($new_array);
        // dd($stocks);
        $stocks->appends(['keyword' =>  $request->keyword,'category' => $request->category, 'brand' => $request->brand, 'user_id' => $request->user_id]);
        return $stocks;
    }

    public function addToCart(Request $request)
    {
        $stock = ProductStock::find($request->stock_id);
        $product = $stock->product;

        $data = array();
        $data['stock_id'] = $request->stock_id;
        $data['id'] = $product->id;
        $data['variant'] = $stock->variant;
        $data['quantity'] = $product->min_qty;

        if($stock->qty < $product->min_qty){
            return array('success' => 0, 'message' => translate("This product doesn't have enough stock for minimum purchase quantity ").$product->min_qty, 'view' => view('pos.cart')->render());
        }

        $tax = 0;
        $price = $stock->price;

        // discount calculation
        $discount_applicable = false;
        if ($product->discount_start_date == null) {
            $discount_applicable = true;
        }
        elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
            strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
            $discount_applicable = true;
        }
        if ($discount_applicable) {
            if($product->discount_type == 'percent'){
                $price -= ($price*$product->discount)/100;
            }
            elseif($product->discount_type == 'amount'){
                $price -= $product->discount;
            }
        }

        //tax calculation
        foreach ($product->taxes as $product_tax) {
            if($product_tax->tax_type == 'percent'){
                $tax += ($price * $product_tax->tax) / 100;
            }
            elseif($product_tax->tax_type == 'amount'){
                $tax += $product_tax->tax;
            }
        }

        $data['price'] = $price;
        $data['tax'] = $tax;

        if($request->session()->has('pos.cart')){
            $foundInCart = false;
            $cart = collect();

            foreach ($request->session()->get('pos.cart') as $key => $cartItem){
                if($cartItem['id'] == $product->id && $cartItem['stock_id'] == $stock->id){
                    $foundInCart = true;
                    $loop_product = Product::find($cartItem['id']);
                    $product_stock = $loop_product->stocks->where('variant', $cartItem['variant'])->first();

                    if($product_stock->qty >= ($cartItem['quantity'] + 1)){
                        $cartItem['quantity'] += 1;
                    }else{
                        return array('success' => 0, 'message' => translate("This product doesn't have more stock."), 'view' => view('pos.cart')->render());
                    }
                }
                $cart->push($cartItem);
            }

            if (!$foundInCart) {
                $cart->push($data);
            }
            $request->session()->put('pos.cart', $cart);
        }
        else{
            $cart = collect([$data]);
            $request->session()->put('pos.cart', $cart);
        }

        $request->session()->put('pos.cart', $cart);

        return array('success' => 1, 'message' => '', 'view' => view('pos.cart')->render());
    }

    //updated the quantity for a cart item
    public function updateQuantity(Request $request)
    {
        $cart = $request->session()->get('pos.cart', collect([]));
        $cart = $cart->map(function ($object, $key) use ($request) {
            if($key == $request->key){
                $product = Product::find($object['id']);
                $product_stock = $product->stocks->where('id', $object['stock_id'])->first();

                if($product_stock->qty >= $request->quantity){
                    $object['quantity'] = $request->quantity;
                }else{
                    return array('success' => 0, 'message' => translate("This product doesn't have more stock."), 'view' => view('pos.cart')->render());
                }
            }
            return $object;
        });
        $request->session()->put('pos.cart', $cart);

        return array('success' => 1, 'message' => '', 'view' => view('pos.cart')->render());
    }

    //removes from Cart
    public function removeFromCart(Request $request)
    {
        if(Session::has('pos.cart')){
            $cart = Session::get('pos.cart', collect([]));
            $cart->forget($request->key);
            Session::put('pos.cart', $cart);

            $request->session()->put('pos.cart', $cart);
        }

        return view('pos.cart');
    }

    //Shipping Address for admin
    public function getShippingAddress(Request $request){
        $user_id = $request->id;
        if($user_id == ''){
            return view('pos.guest_shipping_address');
        }
        else{
            return view('pos.shipping_address', compact('user_id'));
        }
    }

    //Shipping Address for seller
    public function getShippingAddressForSeller(Request $request){
        $user_id = $request->id;
        if($user_id == ''){
            return view('pos.frontend.seller.pos.guest_shipping_address');
        }
        else{
            return view('pos.frontend.seller.pos.shipping_address', compact('user_id'));
        }
    }

    public function set_shipping_address(Request $request) {
        if ($request->address_id != null) {
            $address = Address::findOrFail($request->address_id);
            $data['name'] = $address->user->name;
            $data['email'] = $address->user->email;
            $data['address'] = $address->address;
            $data['country'] = $address->country->name;
            $data['state'] = $address->state->name;
            $data['city'] = $address->city->name;
            $data['postal_code'] = $address->postal_code;
            $data['phone'] = $address->phone;
        } else {
            $data['name'] = $request->name;
            $data['email'] = $request->email;
            $data['address'] = $request->address;
            $data['country'] = Country::find($request->country_id)->name;
            $data['state'] = State::find($request->state_id)->name;
            $data['city'] = City::find($request->city_id)->name;
            $data['postal_code'] = $request->postal_code;
            $data['phone'] = $request->phone;
        }

        $shipping_info = $data;
        $request->session()->put('pos.shipping_info', $shipping_info);
    }

    //set Discount
    public function setDiscount(Request $request){
        if($request->discount >= 0){
            Session::put('pos.discount', $request->discount);
        }
        return view('pos.cart');
    }

    //set Shipping Cost
    public function setShipping(Request $request){
        if($request->shipping != null){
            Session::put('pos.shipping', $request->shipping);
        }
        return view('pos.cart');
    }

    //order summary
    public function get_order_summary(Request $request){
        return view('pos.order_summary');
    }

    //order place
    public function order_store(Request $request){
        if(Session::get('pos.shipping_info') == null || Session::get('pos.shipping_info')['name'] == null || Session::get('pos.shipping_info')['phone'] == null || Session::get('pos.shipping_info')['address'] == null){
            return array('success' => 0, 'message' => translate("Please Add Shipping Information."));
        }

        if(Session::has('pos.cart') && count(Session::get('pos.cart')) > 0){
            $order = new Order;

            $shipping_info = Session::get('pos.shipping_info');
            if ($request->user_id == null) {
                $order->guest_id    = mt_rand(100000, 999999);
            }
            else {
                $order->user_id = $request->user_id;
            }
            $data['name']           = $shipping_info['name'];
            $data['email']          = $shipping_info['email'];
            $data['address']        = $shipping_info['address'];
            $data['country']        = $shipping_info['country'];
            $data['city']           = $shipping_info['city'];
            $data['postal_code']    = $shipping_info['postal_code'];
            $data['phone']          = $shipping_info['phone'];
            $order->shipping_address = json_encode($data);

            $order->payment_type = $request->payment_type;
            $order->delivery_viewed = '0';
            $order->payment_status_viewed = '0';
            $order->code = date('Ymd-His').rand(10,99);
            $order->date = strtotime('now');
            $order->payment_status = $request->payment_type != 'cash_on_delivery' ? 'paid' : 'unpaid';
            $order->payment_details = $request->payment_type;

            if($request->payment_type == 'offline_payment'){
                if($request->offline_trx_id == null){
                    return array('success' => 0, 'message' => translate("Transaction ID can not be null."));
                }
                $data['name']   = $request->offline_payment_method;
                $data['amount'] = $request->offline_payment_amount;
                $data['trx_id'] = $request->offline_trx_id;
                $data['photo']  = $request->offline_payment_proof;
                $order->manual_payment_data = json_encode($data);
                $order->manual_payment = 1;
            }


            $shipping_info = Session::get('pos.shipping_info');
            $order->picking_switch = get_setting('picking_switch');
            if($order->save()){
                $subtotal = 0;
                $tax = 0;
                $productStorehouseTotal = 0;
                foreach (Session::get('pos.cart') as $key => $cartItem){
                    $product_stock = ProductStock::find($cartItem['stock_id']);
                    if ($product_stock){
                        $product = $product_stock->product;
                        $product_variation = $product_stock->variant;

                        $subtotal += $cartItem['price']*$cartItem['quantity'];
                        $tax += $cartItem['tax']*$cartItem['quantity'];

                        // 计算产品仓库的产品货款
                        $originalProduct = null;
                        if ($product->original_id) {
                            $originalProduct = Product::query()->find($product->original_id);
                            if ($originalProduct) {
                                $productStorehouseTotal += cart_product_price($cartItem, $originalProduct, false, false) * $cartItem['quantity'];
                            }
                        }

                        if($cartItem['quantity'] > $product_stock->qty){
                            $order->delete();
                            return array('success' => 0, 'message' => $product->name.' ('.$product_variation.') '.translate(" just stock outs."));
                        }
                        else {
                            $product_stock->qty -= $cartItem['quantity'];
                            $product_stock->save();
                        }

                        $order_detail = new OrderDetail;
                        $order_detail->order_id  =$order->id;
                        $order_detail->seller_id = $product->user_id;
                        $order_detail->product_id = $product->id;
                        $order_detail->payment_status = $request->payment_type != 'cash_on_delivery' ? 'paid' : 'unpaid';
                        $order_detail->variation = $product_variation;
                        $order_detail->price = $cartItem['price'] * $cartItem['quantity'];
                        $order_detail->tax = $cartItem['tax'] * $cartItem['quantity'];
                        $order_detail->quantity = $cartItem['quantity'];
                        $order_detail->shipping_type = null;
                        $order_detail->is_storehouse_product = $product->original_id ? 1 : 0; // 是否产品仓库产品
                        $order_detail->original_product_id = $product->original_id ?: NULL; // 原产品仓库产品ID
                        $order_detail->original_product_price = $originalProduct ? $originalProduct->unit_price : NULL; // 原产品仓库产品价格(进货价)

                        if (Session::get('pos.shipping', 0) >= 0){
                            $order_detail->shipping_cost = Session::get('pos.shipping', 0)/count(Session::get('pos.cart'));
                        }
                        else {
                            $order_detail->shipping_cost = 0;
                        }

                        $order_detail->save();

                        $product->num_of_sale++;
                        $product->save();
                    }
                }

                $order->grand_total = $subtotal + $tax + Session::get('pos.shipping', 0);

                if(Session::has('pos.discount')){
                    $order->grand_total -= Session::get('pos.discount');
                    $order->coupon_discount = Session::get('pos.discount');
                }

                $order->seller_id = $product->user_id;
                $order->product_storehouse_total = $productStorehouseTotal;
                $order->save();

                if ( get_setting('picking_switch') != 1 )
                {//如果不需要提货，直接修改订单为已提货状态
                    $shop = $order->shop;
                    $shop->admin_to_pay += ( $order->grand_total - $order->product_storehouse_total );
                    $shop->save();
                    // 保存订单冻结资金过期时间
                    $freezeDays = get_setting('frozen_funds_unfrozen_days', 15);
                    $order->freeze_expired_at = Carbon::now()->addDays($freezeDays)->timestamp;
                    $order->product_storehouse_status = 1;
                    $order->save();
                }

                $array['view'] = 'emails.invoice';
                $array['subject'] = 'Your order has been placed - '.$order->code;
                $array['from'] = env('MAIL_USERNAME');
                $array['order'] = $order;

                $admin_products = array();
                $seller_products = array();


                foreach ($order->orderDetails as $key => $orderDetail){
                    if(!$orderDetail->product) continue;
                    if($orderDetail->product->added_by == 'admin'){
                        array_push($admin_products, $orderDetail->product->id);
                    }
                    else{
                        $product_ids = array();
                        if(array_key_exists($orderDetail->product->user_id, $seller_products)){
                            $product_ids = $seller_products[$orderDetail->product->user_id];
                        }
                        array_push($product_ids, $orderDetail->product->id);
                        $seller_products[$orderDetail->product->user_id] = $product_ids;
                    }
                }
                foreach($seller_products as $key => $seller_product){
                    try {
                        Mail::to(User::find($key)->email)->queue(new InvoiceEmailManager($array));
                    } catch (\Exception $e) {

                    }
                }

                //sends email to customer with the invoice pdf attached
                if(env('MAIL_USERNAME') != null){
                    try {
                        Mail::to($request->session()->get('pos.shipping_info')['email'])->queue(new InvoiceEmailManager($array));
                        Mail::to(User::where('user_type', 'admin')->first()->email)->queue(new InvoiceEmailManager($array));
                    } catch (\Exception $e) {

                    }
                }

                if($request->user_id != NULL){
                    if (Addon::where('unique_identifier', 'club_point')->first() != null && Addon::where('unique_identifier', 'club_point')->first()->activated) {
                        $clubpointController = new ClubPointController;
                        $clubpointController->processClubPoints($order);
                    }
                }

//                calculateCommissionAffilationClubPoint($order);
                (new CheckoutController())->calculateCommission($order);
                Session::forget('pos.shipping_info');
                Session::forget('pos.shipping');
                Session::forget('pos.discount');
                Session::forget('pos.cart');
                return array('success' => 1, 'message' => translate('Order Completed Successfully.'));
            }
            else {
                return array('success' => 0, 'message' => translate('Please input customer information.'));
            }
        }
        return array('success' => 0, 'message' => translate("Please select a product."));
    }
    public function order_store_into_job(Request $request){
        if(Session::get('pos.shipping_info') == null || Session::get('pos.shipping_info')['name'] == null || Session::get('pos.shipping_info')['phone'] == null || Session::get('pos.shipping_info')['address'] == null){
            return array('success' => 0, 'message' => translate("Please Add Shipping Information."));
        }
        if (!$request->cron_time) {
            return array('success' => 0, 'message' => translate("Please Choose Shipping Information."));
        }

        $jobInputs = [
            'payment_type' => $request->payment_type,
            'cron_time' => $request->cron_time,
            'sign_time' => $request->sign_time,
            'user_id' => $request->user_id,
            'order_no' => date('Ymd-His').rand(10,99),
            'guest_id' => mt_rand(100000, 999999),
            'inputs' => json_encode([
                'cart' => Session::get('pos.cart'),
                'shipping' => Session::get('pos.shipping', 0),
                'discount' => Session::get('pos.discount', 0),
                'shipping_info' => Session::get('pos.shipping_info'),
            ], 256),
            'created_by' => Auth::user()->id,
            'exec_status' => 2,
            'sign_status' => 2,
        ];

        $pay_params = [];
        if($request->payment_type == 'offline_payment'){
            if($request->offline_trx_id == null){
                return array('success' => 0, 'message' => translate("Transaction ID can not be null."));
            }
            $pay_params['name']   = $request->offline_payment_method;
            $pay_params['amount'] = $request->offline_payment_amount;
            $pay_params['trx_id'] = $request->offline_trx_id;
            $pay_params['photo']  = $request->offline_payment_proof;
        }
        $jobInputs['pay_params'] = json_encode($pay_params,256);
        OrderJob::insert($jobInputs);
        Session::forget('pos.shipping_info');
        Session::forget('pos.shipping');
        Session::forget('pos.discount');
        Session::forget('pos.cart');
        return array('success' => 1, 'message' => translate('Order Completed Successfully.'));

    }
    public function order_store_job_exec()
    {
        // 获取分钟内的数据来执行
        $nowTime = Carbon::now()->format('Y-m-d H:i');
        $times = [$nowTime.':00', $nowTime.':59'];
        $lists = OrderJob::whereBetween('cron_time',$times)->where('exec_status', 2)->get()->toArray();
//        $lists = OrderJob::where('exec_status', 2)->get()->toArray();
        $lists2 = OrderJob::whereBetween('sign_time',$times)->where('sign_status', 2)->get()->toArray();
//        $lists2 = OrderJob::where('sign_status', 2)->get()->toArray();
        if (count($lists) > 0) {
            foreach ($lists as $vo) {
                try {
                    $inputs = json_decode($vo['inputs'], 1);
                    $shipping_info = $inputs['shipping_info'];
                    $cart = $inputs['cart'];
                    $shipping = $inputs['shipping'];
                    $discount = $inputs['discount'];
                    $guestId = $vo['guest_id'];
                    $payment_type = $vo['payment_type'];
                    $order_no = $vo['order_no'];
                    $pay_params = json_decode($vo['pay_params'], 1);
                    if(count($cart) > 0){
                        $order = new Order;
                        if ($vo['user_id'] == null) {
                            $order->guest_id    = $guestId;
                        }
                        else {
                            $order->user_id = $vo['user_id'];
                        }
                        $data['name']           = $shipping_info['name'];
                        $data['email']          = $shipping_info['email'];
                        $data['address']        = $shipping_info['address'];
                        $data['country']        = $shipping_info['country'];
                        $data['city']           = $shipping_info['city'];
                        $data['postal_code']    = $shipping_info['postal_code'];
                        $data['phone']          = $shipping_info['phone'];
                        $order->shipping_address = json_encode($data);
                        $order->is_virtually = 1;

                        $order->payment_type = $payment_type;
                        $order->delivery_viewed = '0';
                        $order->payment_status_viewed = '0';
                        $order->code = $order_no;
                        $order->date = strtotime('now');
                        $order->payment_status = $payment_type != 'cash_on_delivery' ? 'paid' : 'unpaid';
                        $order->payment_details = $payment_type;

                        if($payment_type == 'offline_payment'){
                            $data['name']   = $pay_params['offline_payment_method'];
                            $data['amount'] = $pay_params['offline_payment_amount'];
                            $data['trx_id'] = $pay_params['offline_trx_id'];
                            $data['photo']  = $pay_params['offline_payment_proof'];
                            $order->manual_payment_data = json_encode($data);
                            $order->manual_payment = 1;
                        }


                        $order->picking_switch = get_setting('picking_switch');
                        if($order->save()){
                            $subtotal = 0;
                            $tax = 0;
                            $productStorehouseTotal = 0;
                            foreach ($cart as $key => $cartItem){
                                $product_stock = ProductStock::find($cartItem['stock_id']);
                                if ($product_stock){
                                    $product = $product_stock->product;
                                    $product_variation = $product_stock->variant;

                                    $subtotal += $cartItem['price']*$cartItem['quantity'];
                                    $tax += $cartItem['tax']*$cartItem['quantity'];

                                    // 计算产品仓库的产品货款
                                    $originalProduct = null;
                                    if ($product->original_id) {
                                        $originalProduct = Product::query()->find($product->original_id);
                                        if ($originalProduct) {
                                            $productStorehouseTotal += cart_product_price($cartItem, $originalProduct, false, false) * $cartItem['quantity'];
                                        }
                                    }

                                    if($cartItem['quantity'] > $product_stock->qty){
                                        $order->delete();
                                        return array('success' => 0, 'message' => $product->name.' ('.$product_variation.') '.translate(" just stock outs."));
                                    }
                                    else {
                                        $product_stock->qty -= $cartItem['quantity'];
                                        $product_stock->save();
                                    }

                                    $order_detail = new OrderDetail;
                                    $order_detail->order_id  =$order->id;
                                    $order_detail->seller_id = $product->user_id;
                                    $order_detail->product_id = $product->id;
                                    $order_detail->payment_status = $payment_type != 'cash_on_delivery' ? 'paid' : 'unpaid';
                                    $order_detail->variation = $product_variation;
                                    $order_detail->price = $cartItem['price'] * $cartItem['quantity'];
                                    $order_detail->tax = $cartItem['tax'] * $cartItem['quantity'];
                                    $order_detail->quantity = $cartItem['quantity'];
                                    $order_detail->shipping_type = null;
                                    $order_detail->is_storehouse_product = $product->original_id ? 1 : 0; // 是否产品仓库产品
                                    $order_detail->original_product_id = $product->original_id ?: NULL; // 原产品仓库产品ID
                                    $order_detail->original_product_price = $originalProduct ? $originalProduct->unit_price : NULL; // 原产品仓库产品价格(进货价)
                                    if (Session::get('pos.shipping', 0) >= 0){
                                        $order_detail->shipping_cost = $shipping/count($cart);
                                    }
                                    else {
                                        $order_detail->shipping_cost = 0;
                                    }

                                    $order_detail->save();

                                    $product->num_of_sale++;
                                    $product->save();
                                }
                            }

                            $order->grand_total = $subtotal + $tax + $shipping;

                            if($discount){
                                $order->grand_total -= $discount;
                                $order->coupon_discount = $discount;
                            }

                            $order->seller_id = $product->user_id;
                            $order->product_storehouse_total = $productStorehouseTotal;
                            $order->save();

                            if ( get_setting('picking_switch') != 1 )
                            {//如果不需要提货，直接修改订单为已提货状态
                                $shop = $order->shop;
                                $shop->admin_to_pay += ( $order->grand_total - $order->product_storehouse_total );
                                $shop->save();
                                // 保存订单冻结资金过期时间
                                $freezeDays = get_setting('frozen_funds_unfrozen_days', 15);
                                $order->freeze_expired_at = Carbon::now()->addDays($freezeDays)->timestamp;
                                $order->product_storehouse_status = 1;
                                $order->save();
                            }

                            $array['view'] = 'emails.invoice';
                            $array['subject'] = 'Your order has been placed - '.$order->code;
                            $array['from'] = env('MAIL_USERNAME');
                            $array['order'] = $order;

                            $admin_products = array();
                            $seller_products = array();


                            foreach ($order->orderDetails as $key => $orderDetail){
                                if(!$orderDetail->product) continue;
                                if($orderDetail->product->added_by == 'admin'){
                                    array_push($admin_products, $orderDetail->product->id);
                                }
                                else{
                                    $product_ids = array();
                                    if(array_key_exists($orderDetail->product->user_id, $seller_products)){
                                        $product_ids = $seller_products[$orderDetail->product->user_id];
                                    }
                                    array_push($product_ids, $orderDetail->product->id);
                                    $seller_products[$orderDetail->product->user_id] = $product_ids;
                                }
                            }
//                            foreach($seller_products as $key => $seller_product){
//                                try {
//                                    Mail::to(User::find($key)->email)->queue(new InvoiceEmailManager($array));
//                                } catch (\Exception $e) {
//
//                                }
//                            }

                            //sends email to customer with the invoice pdf attached
//                            if(env('MAIL_USERNAME') != null){
//                                try {
//                                    Mail::to($shipping_info['email'])->queue(new InvoiceEmailManager($array));
//                                    Mail::to(User::where('user_type', 'admin')->first()->email)->queue(new InvoiceEmailManager($array));
//                                } catch (\Exception $e) {
//
//                                }
//                            }

                            if($vo['user_id'] != NULL){
                                if (Addon::where('unique_identifier', 'club_point')->first() != null && Addon::where('unique_identifier', 'club_point')->first()->activated) {
                                    $clubpointController = new ClubPointController;
                                    $clubpointController->processClubPoints($order);
                                }
                            }
//                            calculateCommissionAffilationClubPoint($order);
                            (new CheckoutController())->calculateCommission($order);
                            OrderJob::where('id', $vo['id'])->update([
                                'exec_status' => 1,
                                'job_exec_time' => Carbon::now(),
                            ]);
                        }
                        else {
                            OrderJob::where('id', $vo['id'])->update([
                                'exec_status' => 3,
                                'job_exec_time' => Carbon::now(),
                                'job_exec_msg' => translate('Please input customer information.'),
                            ]);
                        }
                    }
                }catch (Exception $exception){
                    OrderJob::where('id', $vo['id'])->update([
                        'exec_status' => 3,
                        'job_exec_time' => Carbon::now(),
                        'job_exec_msg' => $exception->getMessage(),
                    ]);
                }
            }
        }
        if (count($lists2)) {
            foreach ($lists2 as $vo) {
                $orderId = Order::where('code', $vo['order_no'])->value('id');
                if ($orderId) {
                    $this->update_delivery_status($orderId);
                }
            }
        }
        return array('success' => 1, 'message' => 'ok');
    }

    public function update_delivery_status($order_id)
    {
        $order = Order::findOrFail($order_id);
        $order->delivery_viewed = '0';
        $order->delivery_status = 'delivered';
        $status = 'delivered';
        $order->save();

        if ($status == 'delivered') {
            product_storehouse_order_free_up($order);
        }

        foreach ($order->orderDetails as $key => $orderDetail) {

            $orderDetail->delivery_status = $status;
            $orderDetail->save();

            if (addon_is_activated('affiliate_system')) {
                if (($status == 'delivered') &&
                    $orderDetail->product_referral_code) {

                    $no_of_delivered = 0;
                    $no_of_canceled = 0;

                    if ($status == 'delivered') {
                        $no_of_delivered = $orderDetail->quantity;
                    }

                    $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();

                    $affiliateController = new AffiliateController;
                    $affiliateController->processAffiliateStats($referred_by_user->id, 0, 0, $no_of_delivered, $no_of_canceled);
                }
            }
        }

        if (addon_is_activated('delivery_boy')) {
            if (Auth::user()->user_type == 'delivery_boy') {
                $deliveryBoyController = new DeliveryBoyController();
                $deliveryBoyController->store_delivery_history($order);
            }
        }

        return 1;
    }

    public function pos_activation()
    {
        return view('pos.pos_activation');
    }

    public function pos_order_delete(Request $request, $id)
    {
        OrderJob::where('id', $id)->delete();
        flash(translate('deleted successfully'))->success();
        return redirect()->route('salesman.cron.orders.index');
    }
}
